package com.qf.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

/**
 * @author 汤奇
 * @version V1.0
 * @Project BookPro
 * @Package com.qf.filter
 * @Description: 字符集处理乱码过滤器
 *                  请求访问会第一个先进入这个过滤器, 过滤器处理完成放行后才会进入servlet
 * @Date 2022/5/22 9:12
 */
@WebFilter("/*")
public class EncodingFilter implements Filter {


    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        //处理请求对象字符集编码格式
        request.setCharacterEncoding("utf-8");

        //处理响应对象字符集编码
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        //放行请求, 到下一个过滤器
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {

    }
}
